package ui;

import model.Attraction;
import ull.AttractionLogic;
import ull.RoadLogic;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.Objects;


public class AttractionView {
    JFrame jFrame = null;
    AttractionLogic attrationLogic = new AttractionLogic();
    RoadLogic roadLogic=new RoadLogic();
    //添加景点
    public void add(View frame) {
        jFrame = new JFrame("添加景点");
        JPanel jPanel = new JPanel();
        JLabel jLabel1 = new JLabel("添加景点");
        JLabel jLabel2 = new JLabel("景点名称");
        JLabel jLabel3 = new JLabel("景点坐标");
        JLabel jLabel4 = new JLabel("x");
        JLabel jLabel5 = new JLabel("y");
        jLabel1.setFont(new Font("黑体", Font.BOLD, 24));
        jLabel2.setFont(new Font("黑体", Font.BOLD, 18));
        jLabel3.setFont(new Font("黑体", Font.BOLD, 18));
        jLabel4.setFont(new Font("黑体", Font.BOLD, 18));
        jLabel5.setFont(new Font("黑体", Font.BOLD, 18));
        JTextField jTextField1 = new JTextField();
        JTextField jTextField2 = new JTextField();
        JTextField jTextField3 = new JTextField();
        JButton button1 = new JButton("重置");
        JButton button2 = new JButton("取消");
        JButton button3 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button3);
        jPanel.setLayout(null);
        jLabel1.setBounds(175, 10, 100, 100);
        jLabel2.setBounds(50, 70, 100, 100);
        jLabel3.setBounds(50, 120, 100, 100);
        jLabel4.setBounds(150, 120, 100, 100);
        jLabel5.setBounds(250, 120, 100, 100);
        jTextField1.setBounds(150, 105, 150, 30);
        jTextField2.setBounds(165, 150, 50, 30);
        ((AbstractDocument) jTextField2.getDocument()).setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                if (string.matches("[0-9]*")) {
                    super.insertString(fb, offset, string, attr);
                }
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                if (text.matches("[0-9]*")) {
                    super.replace(fb, offset, length, text, attrs);
                }
            }
        });
        jTextField3.setBounds(265, 150, 50, 30);
        ((AbstractDocument) jTextField3.getDocument()).setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                if (string.matches("[0-9]*")) {
                    super.insertString(fb, offset, string, attr);
                }
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                if (text.matches("[0-9]*")) {
                    super.replace(fb, offset, length, text, attrs);
                }
            }
        });
        button1.setBounds(50, 400, 80, 30);
        button2.setBounds(200, 400, 80, 30);
        button3.setBounds(340, 400, 80, 30);
        jFrame.setBounds(800, 0, 500, 530);
        jPanel.add(jLabel1);
        jPanel.add(jLabel2);
        jPanel.add(jLabel3);
        jPanel.add(jLabel4);
        jPanel.add(jLabel5);
        jPanel.add(jTextField1);
        jPanel.add(jTextField2);
        jPanel.add(jTextField3);
        jPanel.add(button1);
        jPanel.add(button2);
        jPanel.add(button3);
        jFrame.add(jPanel);
        jFrame.setVisible(true);
        //重置
        button1.addActionListener(e -> jTextField1.setText(""));
        //取消
        button2.addActionListener(e -> jFrame.dispose());
        //确定
        button3.addActionListener(e -> {

            if (!(jTextField1.getText().replaceAll(" +", "").isEmpty()) &&
                    !(jTextField2.getText().replaceAll(" +", "").isEmpty()) &&
                    !(jTextField3.getText().replaceAll(" +", "").isEmpty())) {
                Attraction att = new Attraction();
                att.setName(jTextField1.getText());
                int x = Integer.parseInt(jTextField2.getText());
                int y = Integer.parseInt(jTextField3.getText());
                att.setX(x);
                att.setY(y);
                if (attrationLogic.checkAttraction(att) && attrationLogic.checkBounds(x, y) && !attrationLogic.ifExist(att.getName())) {
                    try {
                        attrationLogic.addAttraction(att);
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    frame.revalidate();
                    frame.repaint();
                    JOptionPane.showMessageDialog(null, "添加成功！！！");
                    jFrame.dispose();
                    frame.dispose();
                    add(new View());
                } else if (!attrationLogic.checkBounds(x, y)) {
                    JOptionPane.showMessageDialog(jFrame, "超出边界，无法添加", "提示", JOptionPane.ERROR_MESSAGE);
                    jTextField2.setText("");
                    jTextField3.setText("");
                } else if (attrationLogic.ifExist(att.getName())) {
                    JOptionPane.showMessageDialog(jFrame, "已存在该地点，无法添加", "提示", JOptionPane.ERROR_MESSAGE);
                    jTextField1.setText("");
                } else {
                    JOptionPane.showMessageDialog(jFrame, "此处存在建筑物,无法添加", "提示", JOptionPane.INFORMATION_MESSAGE);
                    jTextField2.setText("");
                    jTextField3.setText("");
                }
            } else {
                JOptionPane.showMessageDialog(jFrame, "输入景点不能为空", "提示", JOptionPane.WARNING_MESSAGE);
            }
        });
    }

    //删除景点
    public void del(View frame) {
        jFrame = new JFrame("删除景点");
        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        JLabel jLabel = new JLabel("请选择要删除的景点");
        new JComboBox();
        JComboBox jComboBox;
        String[] str=attrationLogic.getAttractionName();
        jComboBox = new JComboBox(str);
        jLabel.setFont(new Font("黑体", Font.BOLD, 20));
        JButton button1 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button1);
        JButton button2 = new JButton("取消");
        jLabel.setBounds(100, 50, 400, 20);
        button1.setBounds(75, 200, 100, 30);
        button2.setBounds(200, 200, 100, 30);
        jPanel.add(jLabel);
        jPanel.add(button1);
        jPanel.add(button2);
        jPanel.add(jComboBox);
        jComboBox.setBounds(100, 100, 200, 50);
        jFrame.add(jPanel);
        jFrame.setBounds(800, 0, 400, 400);
        jFrame.setVisible(true);
        button2.addActionListener(e -> jFrame.dispose());
        JComboBox finalJComboBox = jComboBox;
        button1.addActionListener(e -> {
            Object selectedItem = finalJComboBox.getSelectedItem();
            String string =selectedItem.toString();
            attrationLogic.deleteRoad1(string);
            attrationLogic.deleteView(string);
            frame.revalidate();
            frame.repaint();
            JOptionPane.showMessageDialog(jFrame, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
            frame.dispose();
            jFrame.dispose();
            del(new View());
        });
    }

    //修改路线
    public void modify(View frame) {
        jFrame = new JFrame("修改路线");
        jFrame.setLayout(null);
        JLabel jLabel = new JLabel("请输入起始点名称");
        JLabel jLabel1 = new JLabel("请输入终点的名称");
        JLabel jLabel2 = new JLabel("请输入两点间的距离");
        new JComboBox();
        JComboBox jComboBox2;
        String[] str=attrationLogic.getAttractionName();
        jComboBox2 = new JComboBox(str);
        JTextField jTextField3 = new JTextField();
        new JComboBox();
        JComboBox jComboBox1;
        String[] str1=attrationLogic.getAttractionName();
        jComboBox1 = new JComboBox(str1);
        jComboBox1.setFont(new Font("黑体", Font.BOLD, 30));
        jTextField3.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel1.setFont(new Font("黑体", Font.BOLD, 30));
        jComboBox2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setBounds(250, 50, 800, 100);
        jLabel1.setBounds(250, 250, 800, 100);
        jLabel2.setBounds(250, 450, 800, 100);
        jComboBox1.setBounds(150, 150, 500, 100);
        jComboBox2.setBounds(150, 350, 500, 100);
        jTextField3.setBounds(150, 550, 500, 100);
        JButton button2 = new JButton("取消");
        JButton button3 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button3);
        button2.setBounds(350, 700, 100, 30);
        button3.setBounds(575, 700, 100, 30);
        jFrame.add(button2);
        jFrame.add(button3);
        jFrame.add(jLabel2);
        jFrame.getContentPane().add(jTextField3);
        jFrame.add(jLabel);
        jFrame.add(jComboBox1);
        jFrame.add(jLabel1);
        jFrame.add(jComboBox2);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setBounds(800, 0, 800, 800);
        jFrame.setVisible(true);
        jTextField3.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {

                String key = "0123456789";
                if (key.indexOf(e.getKeyChar()) < 0) {
                    e.consume();//如果不是数字则取消
                }
            }
        });
        //取消
        button2.addActionListener(e -> jFrame.dispose());
        //确定
        JComboBox finalJComboBox = jComboBox2;
        JComboBox finalJComboBox1 = jComboBox1;
        button3.addActionListener(e -> {
            if (!(jTextField3.getText().replaceAll(" +", "").isEmpty())) {
                String text1 = Objects.requireNonNull(finalJComboBox1.getSelectedItem()).toString();
                String text2 = Objects.requireNonNull(finalJComboBox.getSelectedItem()).toString();
                String text0 = jTextField3.getText();
                int text3 = Integer.parseInt(text0);
                if (text1.equals(text2)) {
                    JOptionPane.showMessageDialog(jFrame, "地址一样，无需修改", "提示", JOptionPane.ERROR_MESSAGE);
                    jTextField3.setText("");
                } else if (attrationLogic.checkDistance(text3)) {
                    JOptionPane.showMessageDialog(jFrame, "距离太大，无法修改", "提示", JOptionPane.ERROR_MESSAGE);
                } else {
                    if (attrationLogic.checkText1(text1, text2)) {
                        attrationLogic.modifyRoad(text1, text2, text3);
                        frame.revalidate();
                        frame.repaint();
                        JOptionPane.showMessageDialog(jFrame, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                        jFrame.dispose();
                        frame.dispose();
                        modify(new View());
                    } else {
                        JOptionPane.showMessageDialog(jFrame, "不存在该路线", "提示", JOptionPane.ERROR_MESSAGE);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(jFrame, "距离不能为空", "提示", JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    //查询线路
    public void select() {
        jFrame = new JFrame("查询路线");
        jFrame.setLayout(null);
        JLabel jLabel = new JLabel("请输入起始点名称");
        JLabel jLabel1 = new JLabel("请输入终点的名称");
        new JComboBox();
        JComboBox jComboBox2;
        String[] str = attrationLogic.getAttractionName();
        jComboBox2 = new JComboBox(str);
        new JComboBox();
        JComboBox jComboBox1;
        String[] str1 = attrationLogic.getAttractionName();
        jComboBox1 = new JComboBox(str1);
        jComboBox1.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel1.setFont(new Font("黑体", Font.BOLD, 30));
        jComboBox2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setBounds(250, 50, 800, 100);
        jLabel1.setBounds(250, 250, 800, 100);
        jComboBox1.setBounds(150, 150, 500, 100);
        jComboBox2.setBounds(150, 350, 500, 100);
        JButton button2 = new JButton("取消");
        JButton button3 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button3);
        jFrame.getRootPane().setDefaultButton(button3);
        button2.setBounds(200, 700, 100, 30);
        button3.setBounds(500, 700, 100, 30);
        jFrame.add(button2);
        jFrame.add(button3);
        jFrame.add(jLabel);
        jFrame.add(jComboBox1);
        jFrame.add(jLabel1);
        jFrame.add(jComboBox2);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setBounds(800, 0, 800, 800);
        jFrame.setVisible(true);
        //取消
        button2.addActionListener(e -> jFrame.dispose());
        //确定
        JComboBox finalJComboBox = jComboBox2;
        JComboBox finalJComboBox1 = jComboBox1;
        button3.addActionListener(e -> {
            String text1 = Objects.requireNonNull(finalJComboBox1.getSelectedItem()).toString();
            String text2 = Objects.requireNonNull(finalJComboBox.getSelectedItem()).toString();
            if (text1.equals(text2)) {
                JOptionPane.showMessageDialog(jFrame, "地址一致，无需查询", "提示", JOptionPane.ERROR_MESSAGE);
            } else {
                selectRoads(text1, text2);
            }
        });
    }

    //查询最短路径
    public void selectRoads(String text1, String text2) {
        jFrame = new JFrame();
        jFrame.setLayout(null);
        JButton jButton=new JButton("返回");
        String string=attrationLogic.getShortestDistance(text1,text2);
        JTextArea jTextField = new JTextArea();
        jTextField.setLineWrap(true);
        jTextField.setFont(new Font("黑体", Font.PLAIN, 20));
        jTextField.setBounds(200, 200, 500, 300);
        jButton.setFont(new Font("黑体", Font.PLAIN, 20));
        jButton.setBounds(350,650,100,50);
        jTextField.setText(string);
        jTextField.setEditable(false);
        jFrame.getRootPane().setDefaultButton(jButton);
        jFrame.add(jTextField);
        jFrame.add(jButton);
        jFrame.setBounds(800, 0, 800, 800);
        jFrame.setVisible(true);
        jButton.addActionListener(e -> jFrame.dispose());
    }

    //添加路线
    public void addRoad(View frame) {
        jFrame = new JFrame("添加路线");
        jFrame.setLayout(null);
        JLabel jLabel = new JLabel("请输入起始点名称");
        JLabel jLabel1 = new JLabel("请输入终点的名称");
        JLabel jLabel2 = new JLabel("请输入两点间的距离");
        JComboBox jComboBox2 = new JComboBox();

        String[] str =attrationLogic.getAttractionName();
        jComboBox2 = new JComboBox(str);
        JTextField jTextField3 = new JTextField();
        new JComboBox();
        JComboBox jComboBox1;
        String[] str1 = attrationLogic.getAttractionName();
        jComboBox1 = new JComboBox(str1);
        jComboBox1.setFont(new Font("黑体", Font.BOLD, 30));
        jTextField3.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel1.setFont(new Font("黑体", Font.BOLD, 30));
        jComboBox2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setBounds(250, 50, 800, 100);
        jLabel1.setBounds(250, 250, 800, 100);
        jLabel2.setBounds(250, 450, 800, 100);
        jComboBox1.setBounds(150, 150, 500, 100);
        jComboBox2.setBounds(150, 350, 500, 100);
        jTextField3.setBounds(150, 550, 500, 100);
        JButton button2 = new JButton("取消");
        JButton button3 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button3);
        button2.setBounds(350, 700, 100, 30);
        button3.setBounds(575, 700, 100, 30);
        jFrame.add(button2);
        jFrame.add(button3);
        jFrame.add(jLabel2);
        jFrame.getContentPane().add(jTextField3);
        jFrame.add(jLabel);
        jFrame.add(jComboBox1);
        jFrame.add(jLabel1);
        jFrame.add(jComboBox2);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setBounds(800, 0, 800, 800);
        jFrame.setVisible(true);
        jTextField3.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {

                String key = "0123456789";
                if (key.indexOf(e.getKeyChar()) < 0) {
                    e.consume();//如果不是数字则取消
                }
            }
        });
        //取消
        button2.addActionListener(e -> jFrame.dispose());
        //确定
        JComboBox finalJComboBox = jComboBox2;
        JComboBox finalJComboBox1 = jComboBox1;
        button3.addActionListener(e -> {
            if (!(jTextField3.getText().replaceAll(" +", "").isEmpty())) {
                String text1 = Objects.requireNonNull(finalJComboBox1.getSelectedItem()).toString();
                String text2 = Objects.requireNonNull(finalJComboBox.getSelectedItem()).toString();
                String text0 = jTextField3.getText();
                int text3 = Integer.parseInt(text0);
                if (text1.equals(text2)) {
                    JOptionPane.showMessageDialog(jFrame, "地址一样，无需添加", "提示", JOptionPane.ERROR_MESSAGE);
                    jTextField3.setText("");
                } else if (attrationLogic.ifExistRoad(text1, text2)) {
                    JOptionPane.showMessageDialog(jFrame, "已存在该路线，无法添加", "提示", JOptionPane.ERROR_MESSAGE);
                    jTextField3.setText("");
                } else if (attrationLogic.checkDistance(text3)) {
                    JOptionPane.showMessageDialog(jFrame, "距离太大，无法添加", "提示", JOptionPane.ERROR_MESSAGE);
                } else {
                    if (attrationLogic.checkText(text1, text2, text3)) {
                        frame.revalidate();
                        frame.repaint();
                        JOptionPane.showMessageDialog(jFrame, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                        jFrame.dispose();
                        frame.dispose();
                        addRoad(new View());
                    }
                }
            } else {
                JOptionPane.showMessageDialog(jFrame, "距离不能为空", "提示", JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    //查看所有景点
    public void selectAttractions() {
        jFrame = new JFrame("景点");
        jFrame.setLayout(new BorderLayout());
        JTextArea jTextArea = new JTextArea();
        jTextArea.setEditable(false);
        jTextArea.setFont(new Font("黑体", Font.BOLD, 30));
        String str=attrationLogic.getAttractionNames();
        jTextArea.append(str);
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        jFrame.add(jScrollPane);
        jFrame.add(jTextArea);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setBounds(800, 0, 800, 800);
        jFrame.setVisible(true);
    }

    //删除路线
    public void deleteRoad(View frame) {
        jFrame = new JFrame("删除路线");
        jFrame.setLayout(null);
        JLabel jLabel = new JLabel("请输入起始点名称");
        JLabel jLabel1 = new JLabel("请输入终点的名称");
        new JComboBox();
        JComboBox jComboBox2;
        String[] str = attrationLogic.getAttractionName();
        jComboBox2 = new JComboBox(str);
        new JComboBox();
        JComboBox jComboBox1;
        String[] str1 = attrationLogic.getAttractionName();
        jComboBox1 = new JComboBox(str1);
        jComboBox1.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel1.setFont(new Font("黑体", Font.BOLD, 30));
        jComboBox2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setBounds(250, 50, 800, 100);
        jLabel1.setBounds(250, 250, 800, 100);
        jComboBox1.setBounds(150, 150, 500, 100);
        jComboBox2.setBounds(150, 350, 500, 100);
        JButton button2 = new JButton("取消");
        JButton button3 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button3);
        jFrame.getRootPane().setDefaultButton(button3);
        button2.setBounds(200, 700, 100, 30);
        button3.setBounds(500, 700, 100, 30);
        jFrame.add(button2);
        jFrame.add(button3);
        jFrame.add(jLabel);
        jFrame.add(jComboBox1);
        jFrame.add(jLabel1);
        jFrame.add(jComboBox2);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setBounds(800, 0, 800, 800);
        jFrame.setVisible(true);
        //取消
        button2.addActionListener(e -> jFrame.dispose());
        //确定
        JComboBox finalJComboBox = jComboBox2;
        JComboBox finalJComboBox1 = jComboBox1;
        button3.addActionListener(e -> {
            String text1 = Objects.requireNonNull(finalJComboBox1.getSelectedItem()).toString();
            String text2 = Objects.requireNonNull(finalJComboBox.getSelectedItem()).toString();
            if (text1.equals(text2)) {
                JOptionPane.showMessageDialog(jFrame, "地址一样,无法删除", "提示", JOptionPane.ERROR_MESSAGE);
            } else {
                if (attrationLogic.checkText1(text1, text2)) {
                    attrationLogic.deleteRoad(text1, text2);
                    frame.revalidate();
                    frame.repaint();
                    JOptionPane.showMessageDialog(jFrame, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                    jFrame.dispose();
                    frame.dispose();
                    deleteRoad(new View());
                } else {
                    JOptionPane.showMessageDialog(jFrame, "不存在该路线");
                }
            }
        });
    }

    //修改景点
    public void modifyAttraction(View frame) {
        jFrame = new JFrame("修改景点");
        JPanel jPanel = new JPanel();
        JLabel jLabel = new JLabel("请选择要修改的景点");
        new JComboBox();
        JComboBox jComboBox;
        String[] str = attrationLogic.getAttractionName();
        jComboBox = new JComboBox(str);
        JLabel jLabel3 = new JLabel("景点坐标");
        JLabel jLabel4 = new JLabel("x");
        JLabel jLabel5 = new JLabel("y");
        jComboBox.setFont(new Font("黑体", Font.BOLD, 24));
        jLabel.setFont(new Font("黑体", Font.BOLD, 24));
        jLabel3.setFont(new Font("黑体", Font.BOLD, 18));
        jLabel4.setFont(new Font("黑体", Font.BOLD, 18));
        jLabel5.setFont(new Font("黑体", Font.BOLD, 18));
        JTextField jTextField2 = new JTextField();
        JTextField jTextField3 = new JTextField();
        JButton button1 = new JButton("重置");
        JButton button2 = new JButton("取消");
        JButton button3 = new JButton("确定");
        jFrame.getRootPane().setDefaultButton(button3);
        jPanel.setLayout(null);
        jLabel.setBounds(50, 50, 400, 50);
        jComboBox.setBounds(300, 50, 100, 50);
        jLabel3.setBounds(50, 120, 100, 100);
        jLabel4.setBounds(150, 120, 100, 100);
        jLabel5.setBounds(250, 120, 100, 100);
        jTextField2.setBounds(165, 150, 50, 30);
        ((AbstractDocument) jTextField2.getDocument()).setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                if (string.matches("[0-9]*")) {
                    super.insertString(fb, offset, string, attr);
                }
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                if (text.matches("[0-9]*")) {
                    super.replace(fb, offset, length, text, attrs);
                }
            }
        });
        jTextField3.setBounds(265, 150, 50, 30);
        ((AbstractDocument) jTextField3.getDocument()).setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                if (string.matches("[0-9]*")) {
                    super.insertString(fb, offset, string, attr);
                }
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                if (text.matches("[0-9]*")) {
                    super.replace(fb, offset, length, text, attrs);
                }
            }
        });
        button1.setBounds(50, 400, 80, 30);
        button2.setBounds(200, 400, 80, 30);
        button3.setBounds(340, 400, 80, 30);
        jFrame.setBounds(800, 0, 500, 530);
        jPanel.add(jLabel);
        jPanel.add(jLabel3);
        jPanel.add(jLabel4);
        jPanel.add(jLabel5);
        jPanel.add(jTextField2);
        jPanel.add(jTextField3);
        jPanel.add(button1);
        jPanel.add(button2);
        jPanel.add(button3);
        jPanel.add(jComboBox);
        jFrame.add(jPanel);
        jFrame.setVisible(true);
        //重置
        button1.addActionListener(e -> {
            jTextField2.setText("");
            jTextField3.setText("");
        });
        //取消
        button2.addActionListener(e -> jFrame.dispose());
        //确定
        JComboBox finalJComboBox = jComboBox;
        button3.addActionListener(e -> {

            if (!(jTextField2.getText().replaceAll(" +", "").isEmpty()) &&
                    !(jTextField3.getText().replaceAll(" +", "").isEmpty())) {
                Attraction att = new Attraction();
                Object selectedItem = finalJComboBox.getSelectedItem();
                String string = (String) selectedItem;
                int x = Integer.parseInt(jTextField2.getText());
                int y = Integer.parseInt(jTextField3.getText());
                att.setName(string);
                att.setX(x);
                att.setY(y);
                if (attrationLogic.checkAttraction(att) && attrationLogic.checkBounds(x, y)) {
                    attrationLogic.modifyAttraction(att);
                    attrationLogic.modifyRoad1(att);
                    attrationLogic.modifyRoad2(att);
                    frame.revalidate();
                    frame.repaint();
                    JOptionPane.showMessageDialog(null, "修改成功！！！");
                    jFrame.dispose();
                    frame.dispose();
                    modifyAttraction(new View());
                } else if (!attrationLogic.checkBounds(x, y)) {
                    JOptionPane.showMessageDialog(jFrame, "超出边界，无法添加！！！", "提示", JOptionPane.ERROR_MESSAGE);
                    jTextField2.setText("");
                    jTextField3.setText("");
                } else {
                    JOptionPane.showMessageDialog(jFrame, "此处存在建筑物,无法修改", "提示", JOptionPane.INFORMATION_MESSAGE);
                    jTextField2.setText("");
                    jTextField3.setText("");
                }
            }
        });
    }
}
